-module('break-shift-cipher').
-export([break/1]).


break(String) ->
    break(String, 0).

break(String, Key)  when is_integer(Key), Key >= 0, Key < 25 ->
    io:format(decrypt(String, Key)),
    case  io:get_line("\ndeciphered? (yes/no) > ") of
	"yes\n" ->
	    "The key is " ++ integer_to_list(Key) ;
	"no\n" -> 
	    break(String, Key + 1)
    end;
break(CipheredString, 25) ->
    decrypt(CipheredString, 25),
    case  io:get_line("deciphered? (yes/no) > ") of
	yes ->
	    "The key is" ++ integer_to_list(25) ;
	no -> 
	    "There are no more possibilities!"
    end.


decrypt([],_Key) ->
    [];
decrypt([H|T], Key) ->
    [decryptElement(H, Key)| decrypt(T,Key)].

decryptElement(Element, Key) when is_integer(Element) , Element >= $A, Element =< $Z ->
    Unit = (((Element - 65) - Key) rem 26 ),
    if
	Unit >=0 -> Unit + 65;
	Unit <0  -> Unit + 26 + 65
    end.


